home *** CD-ROM | disk | FTP | other *** search
- unit ParentMainFormUnit;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- Menus, StdCtrls, ExtCtrls;
-
- type
- TForm1 = class(TForm)
- Memo1: TMemo;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- PipeRead: THandle;
- end;
-
- {$ifdef Ver90}
- //This exception class did not exist in Delphi 2
- EWin32Error = class(Exception);
- {$endif}
-
- var
- Form1: TForm1;
-
- const
- PipeNameFixedPrefix = '\\.\pipe\';
- PipeName = PipeNameFixedPrefix + 'SampleNamedPipe';
- PipeMaxInstances = 1; //only allow 1 pipe
- PipeOutBufferSize = 0; //any size
- PipeInBufferSize = 0; //any size
- PipeTimeout = 0; //don't wait
-
- implementation
-
- uses
- ParentNamedPipeUnit;
-
- {$R *.DFM}
-
- {$ifdef Ver90}
- //This function class did not exist in Delphi 2
- function Win32Check(RetVal: Bool): Bool;
- var
- LastError: DWord;
- begin
- Result := RetVal;
- if not RetVal then
- begin
- LastError := GetLastError;
- if LastError <> Error_Success then
- raise EWin32Error.CreateFmt( 'Win32 Error. Code: %d.'#10'%s',
- [LastError, SysErrorMessage(LastError)])
- else
- raise EWin32Error.Create('A Win32 API function failed')
- end;
- end;
- {$endif}
-
- function LaunchChildApp: THandle;
- const
- ChildApp = 'ChildNamedPipe.Exe';
- var
- SI: TStartupInfo;
- PI: TProcessInformation;
- begin
- GetStartupInfo(SI);
- if not CreateProcess(nil, ChildApp, nil, nil, False,
- 0, nil, nil, SI, PI) then
- raise EWin32Error.Create('Unable to launch ' + ChildApp);
- Result := PI.HProcess
- end;
-
- {This will create a named pipe and get a read and write handle
- back. The intention is for a child app to write to the pipe and for
- this parent app to read from the pipe.}
- procedure TForm1.FormCreate(Sender: TObject);
- const
- PipeBufferSize = 0; //default size
- begin
- PipeRead := CreateNamedPipe(PipeName, Pipe_Access_Inbound, Pipe_Type_Byte,
- PipeMaxInstances, PipeOutBufferSize, PipeInBufferSize, PipeTimeOut, nil);
- if PipeRead = Invalid_Handle_Value then
- raise EWin32Error.Create('Cannot create named pipe');
- //This launches the child process and waits for it to
- //settle down before moving on to the next statement
- WaitForInputIdle(LaunchChildApp, Infinite);
- //Start reader thread off
- with TTestNamedPipe.Create(PipeRead) do
- FreeOnTerminate := True
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- CloseHandle(PipeRead);
- end;
-
- end.
-